package com.example.smartcity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.smartcity.bean.GetInfoBean;
import com.example.smartcity.bean.UserBean;
import com.example.smartcity.net.MyHttpCallBack;
import com.example.smartcity.net.OkhttpHelper;
import com.example.smartcity.tools.SpTools;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 编辑用户资料
 * @author YSJ
 * @date 2021/4/21 : 21:50
 */
public class EditUserInfoActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_user_info);
        try {
            init();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void init() throws Exception {
        //查询用户详细信息 7.1  拿到 ===userId


        new OkhttpHelper().<GetInfoBean>get(
                null,
                "http://124.93.196.45:10002/getInfo",
                this,
                new MyHttpCallBack<GetInfoBean>() {
                    @Override
                    public void onResponse(GetInfoBean data, String json) {
                        Log.e("sssss", data.getUser().getUserId());
                        SpTools.put(EditUserInfoActivity.this,"userId",data.getUser().getUserId());
                        getUserInfo();
                    }
                }
                ,GetInfoBean.class
        );




    }

    private void getUserInfo() {
        OkHttpClient client= new OkHttpClient();
        client.newCall(
                new Request.Builder()
                .url("http://124.93.196.45:10002/system/user/"+SpTools.get(this,"userId"))
                .get().addHeader("Authorization" ,SpTools.get(this,"token"))
                .build()
        ).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {

            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String string = response.body().string();
                Gson gson = new Gson();
                UserBean userBean = gson.fromJson(string, UserBean.class);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        UpDateUserInfo(userBean);
                    }
                });
            }
        });
    }

    private void UpDateUserInfo(UserBean userBean) {
        ImageView heard = findViewById(R.id.image_heard);
        EditText nickName = findViewById(R.id.nickName);
        EditText sex = findViewById(R.id.sex);
        Glide.with(this).load(userBean.getData().getAvatar()).into(heard);
        nickName.setText(userBean.getData().getUserName());
        sex.setText(userBean.getData().getSex());

    }





}